Enemy Ship AI

The OTV Miner will also contain enemy ships that are patrolling the area (aka the screen) and will fire at the player's ship if a Os3 is collected by the player. All enemy ships on the screen will fire one laser at the position of the collected Os3, if the player does not move out of the way, their ship will be hit. Remember three hits of any kind to the player's ship will cause the game to be over.

Enemy Ships are GameObjects and are loaded via the LoadContent function. The code excerpt below is how they are created. The GetRandomPositionOnScreen method can be seen here .
enemeyShips = new List<GameObject>();
for (int i = 0; i < maxEnemyShips; i++)
{
    GameObject ship = new GameObject(Content.Load<Texture2D>("Sprites\\enemyship"));
    ship.IsAlive = true;
    ship.Position = GetRandomPostionOnScreen();
    enemeyShips.Add(ship);
}
When a Os3 element is collected by the player's ship the SendMiningAlert method is called and all alive enemy ships turns and fires a laser toward the position of the Os3 element, which the player's ship is also currently at.
private void SendMiningAlert(GameObject target)
{
    foreach (GameObject ship in this.enemeyShips)
    {
    if (ship.IsAlive)
    {
        double dx = (double)(ship.XPos - target.XPos);
        double dy = (double)(ship.YPos - target.YPos);
        ship.Rotation = -(float)(Math.Atan2(dx, dy));
        FireEnemyLaser(ship);
    }
    }
}
Each enemy ship, if alive, calls the FireEnemyLaser method which adds a new laser to enemyLasers list with proper rotation and velocity vector.
private void FireEnemyLaser(GameObject ship)
{
    GameObject laser = new GameObject(Content.Load<Texture2D>("Sprites\\enemyLaser"));
    laser.IsAlive = true;
    laser.Rotation = ship.Rotation;
    laser.Position = ship.Position;
    double atan = Math.Atan2((float)Math.Cos(ship.Rotation), (float)Math.Sin(ship.Rotation));
    laser.Velocity = new Vector2((float)Math.Cos(atan), -(float)Math.Sin(atan)) * 5.0f;
    enemyLasers.Add(laser);
}
In order to keep the enemy lasers firing toward the target, the following code excerpt was added to the UpdateLasers method.
//update enemy lasers
foreach (GameObject laser in enemyLasers)
{
    if (laser.IsAlive)
    {
        laser.Position += laser.Velocity;
        if (!viewportRect.Contains(new Point((int)laser.XPos, (int)laser.YPos)))
        {
        laser.IsAlive = false;
        continue;
        }
        Rectangle laserRect = new Rectangle(
        (int)laser.XPos,
        (int)laser.YPos,
        laser.Sprite.Width,
        laser.Sprite.Height);
        Rectangle shipRect = new Rectangle(
        (int)theShip.XPos,
        (int)theShip.YPos,
        theShip.Sprite.Width,
        theShip.Sprite.Height);
        if (shipRect.Intersects(laserRect))
        {
        laser.IsAlive = false;
        }
    }
 }
This code also detects if the laser runs off the screen and hits the player's ship. In both cases the laser is killed and no longer displayed.

An enemy ship can be removed or killed off the screen by the player firing a laser and hitting it. This detection also occurs inside the UpdateLasers method, but is called on each laser fired by the player's ship.
    foreach (GameObject ship in enemeyShips)
    {
    if (ship.IsAlive)
    {
        Rectangle shipRect = new Rectangle(
        (int)ship.XPos,
        (int)ship.YPos,
        ship.Sprite.Width,
        ship.Sprite.Height);
        if(laserRect.Intersects(shipRect))
        {
        ship.IsAlive = false;
        laser.IsAlive = false;
        continue;
        }
    }
    }
Both the enemy ships and their lasers need to be added to the Draw function in order to be displayed on the screen. The following excerpt shows the code needed to draw both.
foreach (GameObject laser in this.enemyLasers)
{
    if (laser.IsAlive)
        spriteBatch.Draw(laser.Sprite,
        laser.Position,
        null,
        Color.White,
        laser.Rotation,
        laser.Center, 1.0f,
        SpriteEffects.None, 0);
}
foreach (GameObject enemyShip in this.enemeyShips)
{
    if (enemyShip.IsAlive)
    {
    spriteBatch.Draw(enemyShip.Sprite,
        enemyShip.Position,
        null,
        Color.White,
        enemyShip.Rotation,
        enemyShip.Center, 1.0f,
        SpriteEffects.None, 0);
    }
}
I have attached a screenshot of the enemy ships firing toward the player's ship. The player just collected an Os3 (white) element.
enemyShipFiring.PNG
Enemy Ships Firing